﻿using System;

namespace PluginSample2.BarModel
{
	internal class Bar
	{
		#region Properties

		public decimal Maximum { get; private set; }
		public decimal Minimum { get; private set; }
		public DateTime Start { get; private set; }
		public DateTime End { get; private set; }
		public decimal Open { get; private set; }
		public decimal Close { get; private set; }

		public TimeSpan Duration
		{
			get
			{
				return this.End - this.Start;
			}
		}

		#endregion

		#region Public Methods

		public Bar(DateTime start)
		{
			this.Start = start;
			this.Maximum = decimal.MinValue;
			this.Minimum = decimal.MaxValue;
			this.Open = decimal.MinValue;
		}

		public void AddValue(DateTime time, decimal value)
		{
			this.Maximum = Math.Max(this.Maximum, value);
			this.Minimum = Math.Min(this.Minimum, value);
			if (this.Open == decimal.MinValue)
			{
				this.Open = value;
			}
			this.End = time;
			this.Close = value;
		}

		public void AddMaxAndMinCandidates(DateTime time, decimal maxCandidate, decimal minCandidate)
		{
			this.Maximum = Math.Max(this.Maximum, maxCandidate);
			this.Minimum = Math.Min(this.Minimum, minCandidate);
			if (this.Open == decimal.MinValue)
			{
				this.Open = (maxCandidate + minCandidate) / 2;	
			}
			this.Close = (maxCandidate + minCandidate) / 2;

			this.End = time;
		}

		#endregion
	}
}
